﻿//登陆界面
package com.victim.snake.graphics
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import fl.controls.Button;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	
	import com.victim.snake.util.Loader;
	import flash.display.Bitmap;
	import flash.display.LoaderInfo;
	import flash.events.IOErrorEvent;
	import flash.events.Event;
	

	import com.victim.snake.user.User;
	import com.victim.snake.user.ProcessMsg;

	import flash.text.TextFormat;
	import flash.events.KeyboardEvent;

	public class LandSprite extends Sprite
	{
		public var acount:TextField=new TextField();
		var ps:TextField=new TextField();
		var button:Button=new Button();

		public function LandSprite(){			
			//下载背景并初使化其它页面元素
		    var loader:Loader=new Loader("image/land/land.jpg",this.landImageLoaderEvent);
			 //下载登陆按钮图标
			var landButton:Loader=new Loader("image/land/landButton.jpg",this.landButtonLoaderEvent);
		}
		
		//初使化其它页面元素
        function landImageLoaderEvent(event:Event):void{
			//背景图片
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			
			//输入框文字样式
			var inputTextForma:TextFormat=new TextFormat();
			inputTextForma.bold=true;
			inputTextForma.color="0xffffff";
			inputTextForma.size=18;
			
			
			//用户名输入框
			acount.name = "acount";
			acount.x = 284;
			acount.y = 251;
			acount.width = 220;
			acount.height = 24;
			acount.border = false;
			acount.defaultTextFormat=inputTextForma;
			acount.type = TextFieldType.INPUT;			
			
			
			
			ps.x = 284;
			ps.y = 311;
			ps.width = 220;
			ps.height = 24;
			ps.displayAsPassword = true;
			ps.border = false;
			ps.type = TextFieldType.INPUT;
			ps.defaultTextFormat=inputTextForma;

			button.x = 419;
			button.y = 348;
			button.width=92;
			button.height=31;
			button.label = "";

			button.addEventListener(MouseEvent.CLICK,landBtnClick);

			this.addChildAt(Bitmap(loaderInfo.content),0);
			this.addChildAt(acount,1);
			this.addChildAt(ps,2);
			this.addChildAt(button,3);			
			
			this.addEventListener(KeyboardEvent.KEY_DOWN,onEnterDown);
			
		}
		
		function onEnterDown(e:KeyboardEvent):void{
			if(e.keyCode==13){
				this.landBtnClick(null);
			}
		}
		
		 //登陆按钮图标下载完成
		function landButtonLoaderEvent(event:Event):void{
			//按扭图片
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			button.setStyle("icon",Bitmap(loaderInfo.content));
		}
		
		//帐户名输入框获得焦点方法
		public function acountFocus(e:MouseEvent):void{
			User.getStage().focus=this.acount;
			User.getErrorSprite().removeEventListener(MouseEvent.CLICK,acountFocus);
		}
		
		//帐户名输入框获得焦点方法
		public function psFocus(e:MouseEvent):void{
			User.getStage().focus=this.ps;
			User.getErrorSprite().removeEventListener(MouseEvent.CLICK,psFocus);
		}

		//点击登陆按钮
		public function landBtnClick(event:MouseEvent):void
		{
			if(this.acount.text==""){
				User.getErrorSprite().setErrorString("请输入帐户名");
				User.getErrorSprite().visible=true;
				User.getErrorSprite().closebtn.addEventListener(MouseEvent.CLICK,acountFocus);
				return;
			}
			
			if(this.ps.text==""){
				User.getErrorSprite().setErrorString("请输入密码");
				User.getErrorSprite().visible=true;
				User.getErrorSprite().closebtn.addEventListener(MouseEvent.CLICK,psFocus);
				return;
			}
			
			this.acount.mouseEnabled=false;
			this.ps.mouseEnabled=false;
			var count:String = this.acount.text;//用户名
			User.getPlayer().setUserName(count);
			var ps:String = this.ps.text;//密码
			var strMsg:String = count + ";" + ps;
			//发送消息
			ProcessMsg.socketSend(ProcessMsg.generateUTF8Msg(3,strMsg));
		}

	}

}